<template>
  <view class="order tn-safe-area-inset-bottom">
    <!-- 顶部自定义导航 -->
    <tn-nav-bar
      fixed
      customBack
      :bottomShadow="false"
      backgroundColor="#FFFFFF"
    >
      <view class="tn-flex tn-flex-col-center tn-flex-row-center">
        <text class="tn-text-bold tn-text-xl tn-color-black">XXX工厂</text>
      </view>
    </tn-nav-bar>

    <view
      :style="{ paddingTop: vuex_custom_bar_height - 11 + 'px' }"
      style="position: fixed; background-color: #f0f0; z-index: 999"
    >
      <view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left">
        <view
          class=""
          style="width: 100vw; overflow: hidden; margin-top: 36rpx"
        >
          <tn-tabs
            :list="categoryList"
			name="categoryName"
            :current="current"
            @change="tabChange"
            activeColor="#000"
            :bold="true"
            :fontSize="36"
          ></tn-tabs>
        </view>
      </view>
    </view>
    <view
      class="tn-margin-top-sm"
      :style="{ paddingTop: vuex_custom_bar_height + 44 + 'px' }"
    >
      <view class="" v-if="viewList">
        <view class="" style="padding: 30rpx 20rpx">
          <tn-waterfall
            ref="waterfall"
            v-model="list"
            @finish="handleWaterFallFinish"
          >
            <template v-slot:left="{ leftList }">
              <view
                v-for="(item, index) in leftList"
                :key="item.id"
                class="product__item home-shadow"
                @click="tn('/commPages/product?goodsId='+item.id)"
              >
                <view class="item__image">
                  <tn-lazy-load
                    :threshold="6000"
                    height="100%"
                    :image="item.goodsImg"
                    :index="item.id"
                    imgMode="widthFix"
                  ></tn-lazy-load>
                </view>
                <view class="item__data tn-margin-left-sm tn-margin-right-sm">
                  <view class="item__title-container">
                    <text class="item__title tn-color-cat clamp-text-2">{{
                      item.goodsName
                    }}</text>
                  </view>
                  <view
                    class="tn-flex tn-flex-row-between tn-flex-col-between tn-margin-top-xs"
                  >
                    <view class="justify-content-item tn-color-red">
                      <text class="tn-text-sm">￥</text>
                      <text class="tn-text-xl">{{ item.price }}</text>
                    </view>
                    <view
                      class="justify-content-item tn-color-gray tn-text-center"
                      style="padding-top: 5rpx"
                    >
                      <text
                        class="tn-icon-rocket tn-padding-right-xs tn-text-lg"
                      ></text>
                      <text class="tn-text-df">{{ item.salesVolume }}</text>
                    </view>
                  </view>

                </view>
              </view>
            </template>
            <template v-slot:right="{ rightList }">
              <view
                v-for="(item, index) in rightList"
                :key="item.id"
                class="product__item home-shadow"
                @click="tn('/commPages/product?goodsId='+item.id)"
              >
                <view class="item__image">
                  <tn-lazy-load
                    :threshold="6000"
                    height="100%"
                    :image="item.goodsImg"
                    :index="item.id"
                    imgMode="widthFix"
                  ></tn-lazy-load>
                </view>
                <view class="item__data tn-margin-left-sm tn-margin-right-sm">
                  <view class="item__title-container">
                    <text class="item__title tn-color-cat clamp-text-2">{{
                      item.goodsName
                    }}</text>
                  </view>
                  <view
                    class="tn-flex tn-flex-row-between tn-flex-col-between tn-margin-top-xs"
                  >
                    <view class="justify-content-item tn-color-red">
                      <text class="tn-text-sm">￥</text>
                      <text class="tn-text-xl">{{ item.price }}</text>
                    </view>
                    <view
                      class="justify-content-item tn-color-gray tn-text-center"
                      style="padding-top: 5rpx"
                    >
                      <text
                        class="tn-icon-rocket tn-padding-right-xs tn-text-lg"
                      ></text>
                      <text class="tn-text-df">{{ item.salesVolume }}</text>
                    </view>
                  </view>
              
                </view>
              </view>
            </template>
          </tn-waterfall>
        </view>
        <tn-load-more :status="loadStatus" @click="getGoodsList"></tn-load-more>
      </view>
    </view>
    <view class="tn-tabbar-height"></view>
  </view>
</template>

<script>
export default {
  name: "Comm",
  data() {
    return {
      current: 0,
      categoryList: [
      ],
	  goodsListParams:{categoryId:'',keyword:'',pageSize:8},
	  pageNum:1,
      cardCur: 0,
      loadStatus: "loadmore",
      list: [],
      data: [],
	  viewList:false
    };
  },
  // watch:{
	 //  pageNum:{
		//   immediate: true,
		//   handler(value){
		// 	  this.getGoodsList()
		//   }
	 //  }
  // },
  onLoad() {},
  created() {
	// 获取商品分类
    this.getGoodsCategory();
	this.getGoodsList(true)
  },
  methods: {
    // 跳转
    tn(e) {
      uni.navigateTo({
        url: e,
      });
    },
    // tab选项卡切换
    tabChange(index) {
      this.current = index;
	  this.goodsListParams.categoryId = this.categoryList[index].id
	  this.getGoodsList(true)
    },
	async getGoodsCategory() {
		const data = await this.$t.api.categoryList({});
		this.categoryList = [{categoryName:'全部商品',id:''}].concat(data);
	},
	async getGoodsList(type) {
		if (type) {
			this.loadStatus = "loadmore";
			this.viewList = false
			this.list = []
			this.pageNum = 1
			this.$emit("changeScrollTop", 0)
		}else{
			if(this.loadStatus=="nomore")return
			this.pageNum += 1;
		}
		this.goodsListParams.page=this.pageNum;
		const res = await this.$t.api.goodsList(this.goodsListParams);
		if (res.datas && res.datas.length == 0) {
			this.pageNum-=1;
			this.loadStatus = "nomore";
		}
		this.list = this.list.concat(res.datas)
		if (this.list.length == 0 || this.list.length < this.goodsListParams.pageSize || res.total == this.list.length) this.loadStatus = "nomore";
		this.viewList = true
	},
    // 瀑布流加载完毕事件
    handleWaterFallFinish() {
    },
    // 获取价格整数和小数部分
    getPrice(price) {
      const priceStr = String(price);
      if (priceStr.indexOf(".") !== -1) {
        return priceStr.split(".");
      } else {
        return [priceStr, "00"];
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.order {
  max-height: 100vh;
}

/* 自定义导航栏内容 start */
.custom-nav {
  height: 100%;
  background-color: #ffffff;

  &__back {
    margin: auto 5rpx;
    font-size: 45rpx;
    margin-right: 10rpx;
    margin-left: 30rpx;
    flex-basis: 5%;
  }

  &__search {
    flex-basis: 60%;
    width: 100%;
    height: 100%;

    &__box {
      width: 100%;
      height: 70%;
      padding: 10rpx 0;
      margin: 0 30rpx;
      border-radius: 60rpx 60rpx 0 60rpx;
      font-size: 24rpx;
    }

    &__icon {
      padding-right: 10rpx;
      margin-left: 20rpx;
      font-size: 30rpx;
    }

    &__text {
      // color: #AAAAAA;
    }
  }
}

/* 顶部背景图 start */
.top-backgroup {
  height: 450rpx;
  z-index: -1;

  .backgroud-image {
    width: 100%;
    height: 450rpx;
    z-index: -1;
  }
}
/* 顶部背景图 end */

/* 底部安全边距 start*/
.tn-tabbar-height {
  min-height: 120rpx;
  height: calc(140rpx + env(safe-area-inset-bottom) / 2);
  height: calc(140rpx + constant(safe-area-inset-bottom));
}

/* 阴影 start*/
.home-shadow {
  border-radius: 15rpx;
  box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.07);
}

/* 文字截取*/
.clamp-text-1 {
  -webkit-line-clamp: 1;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  text-overflow: ellipsis;
  overflow: hidden;
}

.clamp-text-2 {
  -webkit-line-clamp: 2;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  text-overflow: ellipsis;
  overflow: hidden;
}

/* 轮播视觉差 start */
.card-swiper {
  height: 330rpx !important;
}

.card-swiper swiper-item {
  width: 750rpx !important;
  left: 0rpx;
  box-sizing: border-box;
  padding: 40rpx 30rpx 30rpx 30rpx;
  overflow: initial;
}

.card-swiper swiper-item .swiper-item {
  width: 100%;
  display: block;
  height: 100%;
  border-radius: 10rpx;
  transform: scale(1);
  transition: all 0.2s ease-in 0s;
  will-change: transform;
  // overflow: hidden;
}

.card-swiper swiper-item.cur .swiper-item {
  transform: none;
  transition: all 0.2s ease-in 0s;
  will-change: transform;
}

.card-swiper swiper-item .swiper-item-text {
  margin-top: -160rpx;
  text-align: center;
  width: 100%;
  display: block;
  height: 50%;
  border-radius: 10rpx;
  transform: translate(100rpx, -60rpx) scale(0.9, 0.9);
  transition: all 0.6s ease 0s;
  will-change: transform;
  overflow: hidden;
}

.card-swiper swiper-item.cur .swiper-item-text {
  margin-top: -220rpx;
  width: 100%;
  transform: translate(0rpx, 0rpx) scale(0.9, 0.9);
  transition: all 0.6s ease 0s;
  will-change: transform;
}

.image-banner {
  display: flex;
  align-items: center;
  justify-content: center;
}
.image-banner image {
  width: 100%;
  height: 100%;
}

/* 轮播指示点 start*/
.indication {
  z-index: 9999;
  width: 100%;
  height: 36rpx;
  position: absolute;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.spot {
  background-color: #ffffff;
  opacity: 0.6;
  width: 10rpx;
  height: 10rpx;
  border-radius: 20rpx;
  top: -70rpx;
  margin: 0 8rpx !important;
  position: relative;
}

.spot.active {
  opacity: 1;
  width: 30rpx;
  background-color: #ffffff;
}

/* 用户头像 start */
.logo-image {
  width: 40rpx;
  height: 40rpx;
  position: relative;
}

.logo-pic {
  background-size: cover;
  background-repeat: no-repeat;
  // background-attachment:fixed;
  background-position: top;
  border: 1rpx solid rgba(255, 255, 255, 0.05);
  // box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
  border-radius: 50%;
  overflow: hidden;
  // background-color: #FFFFFF;
}

/* 头像 start */
.logo-image2 {
  width: 90rpx;
  height: 90rpx;
  position: relative;
}

.logo-pic2 {
  background-size: cover;
  background-repeat: no-repeat;
  // background-attachment:fixed;
  background-position: top;
  box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
  border-radius: 50%;
  overflow: hidden;
  // background-color: #FFFFFF;
}

/* 瀑布流*/
.product__item {
  background-color: #ffffff;
  overflow: hidden;
  margin: 0 10rpx;
  margin-bottom: 20rpx;
  // box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);

  .item {
    /* 图片 start */
    &__image {
      width: 100%;
      height: auto;
      background-color: #ffffff;
      border: 1rpx solid #f8f7f8;
      border-radius: 10rpx 10rpx 0 0;
      overflow: hidden;
    }
    /* 图片 end */

    /* 内容 start */
    &__data {
      padding: 14rpx 0rpx;
    }

    /* 标题 start */
    &__title-container {
      text-align: justify;
      line-height: 38rpx;
      vertical-align: middle;
    }
    &__title {
      font-size: 28rpx;
    }
    /* 标题 end */

    /* 标签 start */
    &__tags-container {
      display: flex;
      flex-direction: row;
      flex-wrap: nowrap;
      align-items: center;
      justify-content: flex-start;
    }
    &__tag {
      margin: 10rpx;
      color: #7c8191;
      background-color: #f3f2f7;
      padding: 4rpx 14rpx 6rpx;
      border-radius: 10rpx;
      font-size: 20rpx;

      &:first-child {
        margin-left: 0rpx !important;
      }
    }
    /* 标签 end */

    /* 价格 start */
    &__price-container {
      font-size: 24rpx;
      color: #e83a30;
    }
    &__price {
      &--unit {
      }
      &--integer {
        font-size: 38rpx;
      }
      &--decimal {
      }
    }
    /* 价格 end */
    /* 内容 end */
  }
}
</style>
